import java.util.HashMap;

public class Test02 {
    public int findMaxLength(int[] nums) {
        for(int i = 0; i < nums.length;i++){
            if(nums[i] == 0) nums[i] = -1;
        }
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0,-1); //哈希表中存入的是 前缀和 和 下标
        int sum = 0, maxLength = 0;
        for(int  i = 0 ; i < nums.length ;i++){
            sum += nums[i];
            if(map.containsKey(sum)){
                maxLength = Math.max(maxLength, i - map.get(sum));
            }else{
                map.put(sum ,i);
            }

        }
        return maxLength;
    }
}
